package team.wuyou.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.wuyou.bean.Result;
import team.wuyou.service.UserService;
import team.wuyou.util.annotation.log.OperLog;
import team.wuyou.util.annotation.log.OperLogConst;
import team.wuyou.util.encryption.MD5Utils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;

    @RequestMapping("login")
    @ResponseBody
    @OperLog(operModul = OperLogConst.LOGIN_DESC,operType =OperLogConst.QUERY ,operDesc = "用户登录")
    public String Login(@RequestParam Map map, HttpSession session){
        System.out.println("进入controller，接收到的值为："+map);
        if (map.get("password") == null) {
            map.replace("password", "");
        }else {
            map.replace("password", MD5Utils.MD5Encode((String) map.get("password"), "utf-8", 10, false));
        }
        System.out.println("最终的password:"+map.get("password"));
        Result login = userService.selectUser(map,session);
        // 获取属性
        String value = session.getAttribute("username")+"";
        System.out.println("session:"+value);
        logger.info("Value retrieved from session: {}", value);
        return JSON.toJSONString(login);
    }
    //注册
    @RequestMapping("/insert")
    @ResponseBody
    @OperLog(operModul = OperLogConst.LOGIN_DESC,operType =OperLogConst.ADD ,operDesc = "普通用户注册")
    public String insert(@RequestParam Map map) {
        System.out.println("进入controller，接收到的值为："+map);
        map.put("account_type","customer");
        Map permissionmap = new HashMap();
        //设置权限
        permissionmap.put("permission_id","3");
        Result login = userService.insert(map,permissionmap);
        return JSON.toJSONString(login);
    }
    //账号注销
    @RequestMapping("/removeuser")
    @ResponseBody
    @OperLog(operModul = OperLogConst.LOGIN_DESC,operType =OperLogConst.ADD ,operDesc = "账号注销")
    public String RemoveUser(@RequestParam Map map) {
        System.out.println("进入controller，接收到的值为：" + map);
        Result login = userService.RemoveUser(map);
        return JSON.toJSONString(login);
    }

    //ClientInfo
    //客户信息查询
    @RequestMapping("/info")
    @ResponseBody
    @OperLog(operModul = OperLogConst.LOGIN_DESC,operType =OperLogConst.ADD ,operDesc = "普通用户注册")
    public String SearchClientInfo(@RequestParam Map map) {
        System.out.println("进入controller，接收到的值为："+map);
        Result login = userService.SearchClientInfo(map);
        return JSON.toJSONString(login);
    }
}
